home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_02_03 / 2n03038a < prev    next >
Text File  |  1991-01-29  |  18KB  |  421 lines

  1.  
  2. /* plot1024.c Copyright 1990 by Gary R. Olhoeft */
  3. /* 
  4.    Compile with the MicroWay NDP C-386 v2.1.0 compiler and run
  5.    with the Phar Lap 386|DOS extender. 
  6. */
  7. #include <dos.h>
  8. #include <stdio.h>
  9. #include <math.h>
  10. #include <grex.h>    /* MicroWay NDP C-386 extended graphics library */
  11. #include <reg8514.h> /* VESA 8514/A registers after Richter & Smith (1990) */
  12. #define outpw(p,v) dx = p; ax = v; asm(dx,ax,"     out     dx,ax")
  13. unsigned char **image();
  14. void free_image();
  15. void plot_image_tseng();
  16. void grey_vga();
  17. void plot_image_hgsc();
  18. void grey_hgsc();
  19. void plot_image_8514();
  20. void grey_8514();
  21. void set_hgsc();
  22.   union REGS reg;                      /* required by DOS INT */
  23.   int _pmode;
  24. /* VESA 8514/A 1024x768x256 timing control register parameters */
  25.   static short  initRegs[] =
  26.       { DISP_CNTL, ADVFUNC_CNTL, V_TOTAL, V_SYNC_STRT,
  27.         V_DISP, H_TOTAL, H_SYNC_STRT, H_DISP,
  28.         H_SYNC_WID, V_SYNC_WID, DISP_CNTL, 0 };
  29.   static short  mode1024x8[] =
  30.       { 0x0053, 0x0007, 0x0660, 0x0600,
  31.         0x05FB, 0x009D, 0x0081, 0x007F,
  32.         0x0016, 0x0008, 0x0033 };
  33.   int _pmode;
  34.   unsigned char hpgl_page;
  35.  
  36. main ()
  37.   {
  38.   reg$eax unsigned short ax, v; /* required by inline assembler */
  39.   reg$edx unsigned short dx, p;
  40.   FILE *stream_in;
  41.   char file_in[80];  
  42.   unsigned char str[4096], **image_array;
  43.   int i,j,k;
  44.   char hstr[20];
  45.   asm("hstdata  equ  0C7000h");  /* HGSC 34010 host addresses */
  46.   asm("hstctrl  equ  0C7D00h");  /* for inline assembler use */
  47.   asm("hstadrl  equ  0C7E00h");
  48.   asm("hstadrh  equ  0C7F00h");  
  49.   _pmode=0x8000;                 /* binary disk access */
  50.  
  51. /* read in the image to be displayed (Jupiter's moon Io from Voyager I) */
  52.     strcpy(file_in,"e:\\img\\c1636828.img\0"); 
  53.     if ((stream_in = fopen(file_in,"r")) == NULL)
  54.        {
  55.          printf("Cannot find File %s\n",file_in); exit(0);
  56.        }
  57.     else
  58.        {
  59.          printf("Reading Image File %s\n",file_in);
  60.          i=fread(str,1,2*836,stream_in); /* ignore PDS header */
  61.          image_array = image(768,1024);  /* create space for image */
  62.          for (i=0; i<768; i++) fread(&image_array[i][0], 1, 836, stream_in);
  63.          i=fclose(stream_in);
  64.        }
  65.      
  66. /* Tseng 4000 STB EM-16 Plus PowerGraph 1024x768x256 */
  67.     blk_mb(str, 0x34, 0xC0000, 500); /* read BIOS area */
  68.     k = -1;
  69.     strcpy(hstr," STB \0");
  70.     for (i=0; i<480; i++) {j = memcmp(str+i,hstr,5); if (j==0) k=j;}
  71.     if (k != -1)
  72.       {
  73.         k = -1;
  74.         strcpy(hstr,"AWARD\0");
  75.         for (i=0; i<480; i++) { j = memcmp(str+i,hstr,5); if (j==0) k = j;}
  76.         if (k != -1) /* Tseng 4000 (STB EM-16 Plus PowerGraph) found */
  77.           {
  78.             reg.b.ah = 0;
  79.             reg.b.al = 56; 
  80.             int386(0x10,®,®);        /* set mode */
  81.             /* set grey scale using MicroWay libGREX palette function */
  82.             /* up to 2. sec */    
  83.             for (i=0; i<256; i++) {j = i>>2; vga_palette(i,j,j,j);}
  84.             plot_image_tseng(image_array); /* plot image in 0.193 sec */
  85.             getch();                       /* wait for keypress */
  86.           }
  87.       }
  88.  
  89. /* IBM 8514/A 1024x768x256 setup after Richter and Smith (1990) */
  90.     outp(DAC_MASK, 0x00); /* blank screen by turning all planes off */
  91.     outpw(SUBSYS_CNTL, GPCTRL_RESET | CHPTEST_NORMAL); /* reset 8514/A */
  92.     outpw(SUBSYS_CNTL, GPCTRL_ENAB | CHPTEST_NORMAL);
  93.     outpw(ERR_TERM, 0xFFFF);      /* test for 8514/A card */
  94.     if (inpw(ERR_TERM) == 0xFFFF) /* 8514/A card found */
  95.       {
  96.         i = inpw(SUBSYS_STAT);        /* get board status information */
  97.         if ((i & MONITORID_MASK) == MONITORID_8514) /* 1024x768 monitor */
  98.           {
  99.             outpw(MULTIFUNC_CNTL, MEM_CNTL | VRTCFG_4 | HORCFG_8);
  100.             i = 0; /* setup 1024x768 timing parameters */
  101.             while (initRegs[i]) { outpw(initRegs[i], mode1024x8[i]); i++;}
  102.             outpw(DAC_MASK, 0xFF);        /* enable all 8 planes */
  103.             outpw(FRGD_MIX, FSS_FRGDCOL | MIX_REPLACE);
  104.             outpw(BKGD_MIX, BSS_BKGDCOL | MIX_REPLACE);
  105.             /* clip to full drawable space (0,0 to 1023,1023) */
  106.             outpw(MULTIFUNC_CNTL, SCISSORS_T | 0x000);
  107.             outpw(MULTIFUNC_CNTL, SCISSORS_L | 0x000);
  108.             outpw(MULTIFUNC_CNTL, SCISSORS_B | 0x3FF);
  109.             outpw(MULTIFUNC_CNTL, SCISSORS_R | 0x3FF);
  110.             outpw(WRT_MASK, 0xFFFF);      /* enable writes to all planes */
  111.             outpw(MULTIFUNC_CNTL, PIX_CNTL | 0); /* clear lower bits */
  112.             WaitQueue(6);                 /* wait until 8514 queue ready */
  113.             outpw(FRGD_COLOR, 0);         /* set color */
  114.             outpw(CUR_X, 0);              /* set origin */
  115.             outpw(CUR_Y, 0);
  116.             outpw(MAJ_AXIS_PCNT, 1023);   /* set size */
  117.             outpw(MULTIFUNC_CNTL, MIN_AXIS_PCNT | 1023);
  118.             outpw(CMD, CMD_RECT | INC_Y | INC_X | DRAW | PLANAR | WRTDATA);
  119.             WaitQueue(4);
  120.             outpw(CUR_X, 0);              /* reset draw position to (0,0) */
  121.             outpw(CUR_Y, 0);
  122.             outpw(FRGD_COLOR, 0xFFFF);    /* foreground all on */
  123.             outpw(BKGD_COLOR, 0);         /* background all off */
  124.             grey_8514();                  /* setup 8514 grey scale 0.00087 sec*/
  125.             outpw(FRGD_MIX, FSS_PCDATA | MIX_REPLACE);
  126.             plot_image_8514(image_array); /* plot image in 0.385 sec */
  127.             getch();                      /* wait for key press */
  128.             outpw(ADVFUNC_CNTL, 6);       /* restore 8514/A to VGA */
  129.           }
  130.       }
  131.  
  132. /* Hercules Graphics Station Card GB1024 TI34010 1024x768x256 */
  133.     blk_mb(str, 0x34, 0xC0000, 500); /* read BIOS area */
  134.     k = -1;
  135.     strcpy(hstr,"Hercules\0");
  136.     for (i=0; i<480; i++) {j = memcmp(str+i,hstr,8); if (j==0) k=j;}
  137.     if (k != -1)
  138.       {
  139.         k = -1;
  140.         strcpy(hstr,"Graphics Station\0");
  141.         for (i=0; i<480; i++) { j = memcmp(str+i,hstr,16); if (j==0) k = j;}
  142.         if (k != -1)                       /* HGSC found */
  143.           {
  144.             set_hgsc();                    /* set 34010 mode 1024x768 */
  145.             grey_hgsc();                   /* setup grey scale 0.00082 sec */
  146.             plot_image_hgsc(image_array);  /* plot image in 0.275 sec */
  147.             getch();                       /* wait for key press */
  148.             setvga();                      /* restore HGSC to VGA modes */
  149.             /* setvga is found in TECH Specialist, v.1, n.5, p.35 */
  150.           }
  151.       }
  152.  
  153.     free_image(image_array, 768);     /* free image memory */
  154.     set_video_mode(0x02);             /* restore text mode */
  155.  
  156. unsigned char **image(row,col)
  157. int row,col;
  158. {   /* allocates 8-bit 2D image matrix with range [0...row-1][0...col-1] */
  159.     int i;
  160.     unsigned char **m; /* no error checking: assumes enough memory exists */
  161.     m = (unsigned char **) malloc((unsigned)(row)*sizeof(unsigned char *));
  162.     for (i=0; i<row; i++)
  163.        m[i] = (unsigned char *) malloc((unsigned)(col)*sizeof(unsigned char));
  164.     return m;
  165. }
  166.  
  167. void free_image(m,row)
  168. unsigned char **m;
  169. int row;
  170. {        /* frees memory malloc'd by image function in Listing 2 */
  171.     int i;
  172.     for (i=row-1; i>=0; i--) free(m[i]);
  173.     free(m);
  174. }
  175.  
  176. void plot_image_tseng(image_array) /* Tseng 4000 chipset 1024x768x256 */
  177. unsigned char **image_array;
  178.     reg$eax unsigned short ax; /* required by inline assembler */
  179.     reg$esi unsigned esi;
  180.     reg$edi unsigned edi;
  181.     reg$ecx unsigned ecx;
  182.     int j, jj, k, vcol, row;
  183.     unsigned int cm;
  184.     unsigned short pel;
  185.     vcol = 1024;                        /* length of horizontal screen row */
  186.     row = -vcol;
  187.     asm("     push   es");              /* save es register */
  188.     asm("     mov    ax, 034h");        /* set Phar Lap LDT to access */
  189.     asm("     mov    es, ax");          /* first megabyte of real memory */
  190.     hpgl_page = 99;                     /* force first page */
  191.     for (j=0; j<768; j++)               /* vertical lines (rows) on screen */
  192.       {
  193.         row += vcol;
  194.         ax = (unsigned char)(row >> 16);/* divide by